/**
 * Created by dqw on 2015/12/30.
 */
//列表开始
var investAccount = '';
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'userId',
        type: 'number',
        hideQuery: true,
        'export': false,
        hide: true
    },
    {
        id: 'id',
        title: 'ID',
        type: 'number',
        columnClass: 'text-center width-100',
        fastSort: false
    },
    {
        id: 'u.userName',
        title: '用户名',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center width-200',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'u.mobilePhone',
        title: '电话号码',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center width-200',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'feeScale',
        title: '佣金',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: false,
        format:'#,##0.00000'
    },
    {
        id: 'investAccount',
        title: '投资余额',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: false,
        format:'#,##0.00',
        fastQueryType: 'lk'
    },
    {
        id: 'receiveCount',
        title: '接单数',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: false,
        format:'#,##0',
        fastQueryType: 'lk'
    },
    // {
    //     id: 'buyTimeAgv',
    //     title: '平均买入(秒)',
    //     type: 'string',
    //     headerClass: 'text-center',
    //     columnClass: 'text-center',
    //     fastSort: false,
    //     fastQuery: false,
    //     format:'#,##0',
    //     fastQueryType: 'lk'
    // },
    // {
    //     id: 'sellTimeAgv',
    //     title: '平均卖出(秒)',
    //     type: 'string',
    //     headerClass: 'text-center',
    //     columnClass: 'text-center',
    //     fastSort: false,
    //     fastQuery: false,
    //     format:'#,##0',
    //     fastQueryType: 'lk'
    // },
    // {
    //     id: 'buyTimeoutCount',
    //     title: '买入超时单数',
    //     type: 'string',
    //     headerClass: 'text-center',
    //     columnClass: 'text-center',
    //     fastSort: false,
    //     fastQuery: false,
    //     format:'#,##0',
    //     fastQueryType: 'lk'
    // },
    // {
    //     id: 'sellTimeoutCount',
    //     title: '卖出超时单数',
    //     type: 'number',
    //     headerClass: 'text-center',
    //     columnClass: 'text-center',
    //     fastSort: false,
    //     fastQuery: false,
    //     format:'#,##0',
    //     fastQueryType: 'lk'
    // },

    {
        id: 'operation',
        title: '资金',
        type: 'string',
        columnClass: 'text-center width-200',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
                var editHtml = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;变动&nbsp;</a>";
                var html = "";
                // 判断是否有编辑权限
                if ($("#editPermi") && $("#editPermi").val() == 1) {
                    html += editHtml;
                }
                return html;
        }

    }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'invest/list.json',
    exportFileName: '投资人列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int:["id","userId"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;

//排序
grid.sortParameter.columnId = ['asc_id'];
// grid.sortParameter.sortType = 1;
//列表结束

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {

        //新增对话框初始化
        $("#addModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#addForm").psly().reset();

            $.each($("#addForm input"),function (i, v) {
                $(v).val('');
            })
        });

        //编辑对话框初始化
        $("#editModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#editForm").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];
            investAccount = gridData.investAccount;

            $("#investId").val(gridData.id);
            $("#adminName").val(gridData.name);
            modal.find("[name='password']").val("");
            modal.find("[name='password2']").val("");
            modal.find("[name='roleId']").val(gridData.roleId);
            modal.find("[name='investAccount']").val('');
            $(".type").bootstrapSwitch('state', true);
        });

        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['lk_u.user_name_or_like_u.mobile_phone'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }

    //外部可调用
    return {
        bindEvent: _bindEvent
    }
}();
//操作处理结束

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
});